package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.model.AbMenuItem;
import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.soap.AbSoapUtil;
import com.ab.task.AbTask;
import com.ab.task.AbTaskItem;
import com.ab.task.AbTaskListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.pullview.AbPullToRefreshView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.adapter.ListPopAdapter;
import com.htandroid.htandroid.entity.Htzc_Pvouch;
import com.htandroid.htandroid.entity.Htzc_Pvouchs;
import com.htandroid.htandroid.entity.Stringreturn;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.webservice.BuildHtzcPvouchsXMLService;
import com.htandroid.htandroid.webservice.XMLHtzcPvouchslist;
import com.htandroid.htandroid.webservice.abXMLHtzcPvouchslist;
import com.htandroid.htandroid.webservice.abXMLZcpdPersonslist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

/**
 * Created by lkp on 2015/12/4.
 */
public class zcpd_detail extends AbActivity {
    public MyApplication myApplication;
    private ArrayList<Htzc_Pvouchs> zcpdlist_detail;
    private int icurrentrow;
    private Htzc_Pvouch zcpdtitle;
    private Htzc_Pvouchs zcpvouchs;
    private int textcolor_old;
    private String listtype = "0";

    private AbTitleBar mAbTitleBar = null;
    private View rightView1 = null;
    private View rightView2 = null;
    private View rightView3 = null;
    private TextView tv_currentrow;

    private AbLoadDialogFragment mDialogFragment = null;
    private PopupWindow popupWindow;
    private ListView popListView;
    private View popView;

    private TextView tvTab1;
    private TextView tvTab2;
    private View linetab1;
    private View linetab2;
    private String djtype = "0";

    @AbIocView(id = R.id.zccheckedname_tv)
    TextView zccheckedname_tv;
    @AbIocView(id = R.id.zccheckeddate_tv)
    TextView zccheckeddate_tv;
    @AbIocView(id = R.id.scardnum_tv)
    TextView scardnum_tv;
    @AbIocView(id = R.id.sassetnum_tv)
    TextView sassetnum_tv;
    @AbIocView(id = R.id.sassetname_tv)
    TextView sassetname_tv;
    @AbIocView(id = R.id.sstyle_tv)
    TextView sstyle_tv;
    @AbIocView(id = R.id.dblvalue_tv)
    TextView dblvalue_tv;
    @AbIocView(id = R.id.sdeptnum_tv)
    TextView sdeptnum_tv;
    @AbIocView(id = R.id.ssite_tv)
    TextView ssite_tv;
    @AbIocView(id = R.id.mPullRefreshView)
    AbPullToRefreshView mAbPullToRefreshView;
    @AbIocView(id = R.id.creason_et)
    EditText creason_et;
    @AbIocView(id = R.id.code_et)
    EditText code_et;
    @AbIocView(id = R.id.btn1_tv, click = "btnClick")
    TextView btn1_tv;
    @AbIocView(id = R.id.btn2_tv, click = "btnClick")
    TextView btn2_tv;
    @AbIocView(id = R.id.zccheckedname)
    LinearLayout zccheckedname;
    @AbIocView(id = R.id.zccheckeddate)
    LinearLayout zccheckeddate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_zcpd_detail);
        myApplication = (MyApplication) abApplication;//获得我们的应用程序全局变量
        initview();
    }

    public void btnClick(View v) {
        switch (v.getId()) {
            case R.id.btn1_tv:
                btn1_click(v);
                break;
            case R.id.btn2_tv:
                btn2_click(v);
                break;
        }
    }

    //初始化
    private void initview() {
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("盘点资产明细");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.setTitlePadding(118, 0, 0, 0);

        mAbTitleBar.clearRightView();
        rightView1 = mInflater.inflate(R.layout.btn_abs_back, null);
        mAbTitleBar.addRightView(rightView1);
        rightView2 = mInflater.inflate(R.layout.tv_currentrow, null);
        mAbTitleBar.addRightView(rightView2);
        rightView3 = mInflater.inflate(R.layout.btn_abs_forward, null);
        mAbTitleBar.addRightView(rightView3);
        tv_currentrow = (TextView) rightView2.findViewById(R.id.tv_currentrow);
        tv_currentrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                more_list(v);
            }
        });

        tvTab1 = (TextView) findViewById(R.id.tab1);
        tvTab2 = (TextView) findViewById(R.id.tab2);
        linetab1 = (View) findViewById(R.id.tab1_line);
        linetab2 = (View) findViewById(R.id.tab2_line);
        tvTab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTab1.setTextColor(getResources().getColor(R.color.yellow));
                linetab1.setBackgroundColor(getResources().getColor(R.color.yellow));
                tvTab2.setTextColor(getResources().getColor(R.color.white));
                linetab2.setBackgroundColor(getResources().getColor(R.color.white));
                djtype = "0";//状态一
                zccheckedname.setVisibility(View.GONE);
                zccheckeddate.setVisibility(View.GONE);
                refreshTask();
            }
        });

        tvTab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTab2.setTextColor(getResources().getColor(R.color.yellow));
                linetab2.setBackgroundColor(getResources().getColor(R.color.yellow));
                tvTab1.setTextColor(getResources().getColor(R.color.white));
                linetab1.setBackgroundColor(getResources().getColor(R.color.white));
                zccheckedname.setVisibility(View.VISIBLE);
                zccheckeddate.setVisibility(View.VISIBLE);
                djtype = "1";
                refreshTask();
            }
        });

        mAbPullToRefreshView.getHeaderView().setHeaderProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));
        mAbPullToRefreshView.getFooterView().setFooterProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));

        refreshTask();
        code_et.setImeOptions(EditorInfo.IME_ACTION_DONE);
        //条码输入的监听
        code_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            int touch_flag = 0;

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    if (code_et.getText() == null || code_et.getText().toString().equals(""))
                        return false;
                    touch_flag++;
                    if (touch_flag == 2 || actionId == EditorInfo.IME_ACTION_DONE) {
                        input_code();
                        touch_flag = 0;
                    }
                    return true;
                }
                return false;
            }
        });

        //读取文本颜色
        textcolor_old = creason_et.getCurrentTextColor();
        //进入窗口时,所有控件处于不可编辑状态,下拉隐藏
        showwin(false);
        //获取传递的对象参数
        zcpdtitle = (Htzc_Pvouch) getIntent().getSerializableExtra("zcpd");
        listtype = getIntent().getExtras().getString("listtype");

        popView = mInflater.inflate(R.layout.list_pop, null);
        popListView = (ListView) popView.findViewById(R.id.pop_list);
        popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 1, 1, Animation.ZORDER_BOTTOM, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animationSet.setDuration(250);
        animationSet.addAnimation(scaleAnimation);
        popView.startAnimation(animationSet);
        ImageView more_close = (ImageView) popView.findViewById(R.id.item_close);
        more_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

    public void refreshTask() {
        AbTask mAbTask = new AbTask();
        final AbTaskItem item = new AbTaskItem();
        item.setListener(new AbTaskListener() {

            @Override
            public void update() {
                //参数
//                String[] parms = {"cacc_id", "iyear", "cvouchid"};
//                String[] vals = {(String) myApplication.getGs_ztid(), (String) myApplication.getGs_year(), zcpdtitle.getCvouchid()};
//                //生成xml
//                BuildQgdListXMLService service = new BuildQgdListXMLService();
//                String xmlstring = service.buildXML(null, "zcpd_detaillist", InstorageURL.NAME_SPACE, parms, vals, null);
//                ToolSOAPxml.callService("zcpd_detaillist", xmlstring, new ToolSOAPxml.WebServiceCallBack() {
//                    @Override
//                    public void onSucced(String result) {
//                        if (result != null) {
//                            //解析xml文件,返回列表
//                            zcpdlist_detail = XMLHtzcPvouchslist.paraseCommentInfors(result);
//                            //20160728修改:默认显示第一行未盘点的数据，若都盘点则默认显示第一行数据
//                            if (zcpdlist_detail.size() > 0) {
//                                int viewrow = 0;
//                                for (int i = 0; i < zcpdlist_detail.size(); i++) {
//                                    Htzc_Pvouchs zcpvouchs_view = zcpdlist_detail.get(i);
//                                    if (!(Integer.valueOf(zcpvouchs_view.getLnum().toString()) > 0)) {
//                                        viewrow = i;
//                                        break;
//                                    }
//                                }
//                                insert_data(viewrow);
//                            }
//                        } else {
//                            AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                        }
//                        AbDialogUtil.removeDialog(zcpd_detail.this);
//                    }
//
//                    @Override
//                    public void onFailure(String result) {
//                        AbDialogUtil.removeDialog(zcpd_detail.this);
//                        AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                    }
//                });
//                mAbPullToRefreshView.onHeaderRefreshFinish();

                String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
                AbSoapParams params = new AbSoapParams();
                params.put("arg0", myApplication.getGs_ztid());
                params.put("arg1", myApplication.getGs_year());
                params.put("arg2", zcpdtitle.getCvouchid());
                params.put("arg3", djtype);
                mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "ZcpdDetailList", params, new AbSoapListener() {
                    //获取数据成功会调用这里
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        if (content != null) {
                            zcpdlist_detail = abXMLHtzcPvouchslist.paraseCommentInfors(content);
                            //20160728修改:默认显示第一行未盘点的数据，若都盘点则默认显示第一行数据
                            if (zcpdlist_detail.size() > 0) {
                                int viewrow = 0;
                                for (int i = 0; i < zcpdlist_detail.size(); i++) {
                                    Htzc_Pvouchs zcpvouchs_view = zcpdlist_detail.get(i);
                                    if (!(Integer.valueOf(zcpvouchs_view.getLnum().toString()) > 0)) {
                                        viewrow = i;
                                        break;
                                    }
                                }
                                insert_data(viewrow);
                            }
                        } else {
                            AbToastUtil.showToast(zcpd_detail.this, R.string.sqlerrormsg);
                        }
                        AbDialogUtil.removeDialog(zcpd_detail.this);
                    }

                    // 失败，调用
                    @Override
                    public void onFailure(int statusCode, String content,
                                          Throwable error) {
                        AbToastUtil.showToast(zcpd_detail.this, R.string.sqlerrormsg);
                    }

                    // 开始执行前
                    @Override
                    public void onStart() {
                        //显示进度框
                        AbDialogUtil.showProgressDialog(zcpd_detail.this, 0, "正在加载...");
                    }

                    // 完成后调用，失败，成功
                    @Override
                    public void onFinish() {
                        //移除进度框
                        AbDialogUtil.removeDialog(zcpd_detail.this);
                    }
                });
                mAbPullToRefreshView.onHeaderRefreshFinish();
            }

            @Override
            public void get() {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            }
        });
        mAbTask.execute(item);
    }

    //为窗口填充数据
    public void insert_data(int listrow) {
        if (djtype.equals("0")) {
            //未定价时,默认进入的就是修改状态。经过测试，发现之前的操作模式太过繁琐
            showwin(true);
        } else {
            if (zcpvouchs.getZccheckedname() == "") {

            } else {
                zccheckedname_tv.setText(zcpvouchs.getZccheckedname());
            }
            if (zcpvouchs.getZccheckeddate() == "") {

            } else {
                zccheckeddate_tv.setText(zcpvouchs.getZccheckeddate());
            }
            showwin(false);
        }
        //清空扫描内容
        code_et.setText("");
        zcpvouchs = zcpdlist_detail.get(listrow);
        icurrentrow = listrow;
        //行号
        tv_currentrow.setText((icurrentrow + 1) + "/" + (zcpdlist_detail.size()));
        //填充数据
        //不可修改部分
        scardnum_tv.setText(zcpvouchs.getScardnum());
        sassetnum_tv.setText(zcpvouchs.getSassetnum());
        sassetname_tv.setText(zcpvouchs.getSassetname());
        sstyle_tv.setText(zcpvouchs.getsStyle());
        dblvalue_tv.setText(zcpvouchs.getDblvalue());
        sdeptnum_tv.setText(zcpvouchs.getSdeptname());
        sdeptnum_tv.setTag(zcpvouchs.getSdeptnum());
        ssite_tv.setText(zcpvouchs.getSsite());
        //可修改部分
        creason_et.setText(zcpvouchs.getCreason());
        RadioGroup lnum_rg = (RadioGroup) findViewById(R.id.lnum_rg);
        if (Basenvo.equal(zcpvouchs.getLnum(), "0")) {
            lnum_rg.check(R.id.no_rb);
        } else {
            lnum_rg.check(R.id.yes_rb);
        }
        //扩展信息
        insert_data_detail();
    }

    public void insert_data_detail() {
        ImageView titlemore_iv = (ImageView) findViewById(R.id.listmore_iv);
        titlemore_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ImageView img = (ImageView) v;
                //构造数据
                Map<AbMenuItem, AbMenuItem> map1 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map2 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map3 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map4 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map5 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map6 = new HashMap<AbMenuItem, AbMenuItem>();
                map1.put(new AbMenuItem("　单据号："), new AbMenuItem(zcpdtitle.getCvouchid()));
                map2.put(new AbMenuItem("盘点日期："), new AbMenuItem(zcpdtitle.getDdate()));
                map3.put(new AbMenuItem("盘点部门："), new AbMenuItem(zcpdtitle.getSdepts()));
                map4.put(new AbMenuItem("备　　注："), new AbMenuItem(zcpdtitle.getCmemo()));
                map5.put(new AbMenuItem("　制单人："), new AbMenuItem(zcpdtitle.getCmaker()));
                map6.put(new AbMenuItem("　审核人："), new AbMenuItem(zcpdtitle.getCchecker()));
                ArrayList<Map<AbMenuItem, AbMenuItem>> data = new ArrayList<Map<AbMenuItem, AbMenuItem>>();
                data.add(map1);
                data.add(map2);
                data.add(map3);
                data.add(map4);
                data.add(map5);
                data.add(map6);
                ListPopAdapter mListPopAdapter = new ListPopAdapter(zcpd_detail.this, data, R.layout.list_popitem);
                popListView.setAdapter(mListPopAdapter);

                //弹出窗口后,变更更多图标
                img.setImageResource(R.drawable.item_more_choose);
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    public void onDismiss() {
                        //还原图标
                        img.setImageResource(R.drawable.item_more);
                    }
                });
                popupWindow.showAsDropDown(v);
            }
        });
    }

    //存货切换
    public void more_list(View v) {
        Intent intent = new Intent(getApplicationContext(), zcpd_detaillist.class);
        //传递参数
        Bundle bundle = new Bundle();
        intent.putExtra("cinvcode", zcpvouchs.getScardnum());
        bundle.putSerializable("detaillist", zcpdlist_detail);
        bundle.putSerializable("zcpdtitle", zcpdtitle);//单据表头
        bundle.putSerializable("listtype", listtype);//
        intent.putExtras(bundle);
        intent.putExtra("currentrow", "" + icurrentrow);//当前存货行号
        startActivityForResult(intent, R.layout.layout_zcpd_detaillist / 300000);
    }

    //调用照相机扫描
    public void scancode_click(View v) {
        btn1_click(v);
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), Basenvo_Capture.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        bundle.putInt("cameratype", 0);
        startActivityForResult(intent, 1);
    }

    public boolean input_code() {
        int i = 0;
        //查找当前盘点单是否存在该资产
        for (i = 0; i < zcpdlist_detail.size(); i++) {
            //判断相等
            if (Basenvo.equal(zcpdlist_detail.get(i).getScardnum(), code_et.getText().toString())) {
                //找到则跳出
                break;
            }
            if (Basenvo.equal(zcpdlist_detail.get(i).getSassetnum(), code_et.getText().toString())) {
                //找到则跳出
                break;
            }
        }
        if (i < zcpdlist_detail.size()) {
            //存在则跳转到该行,若之前已经盘点过也做一个提示
            if (Integer.valueOf(zcpdlist_detail.get(i).getLnum().toString()) > 0) {
                AbToastUtil.showToastInThread(getApplicationContext(), "该资产之前已扫描,请确定是否重复扫描!");
            }
            //保存当前界面内容
            updatedataref();
            // 修改为状态为已盘点
            zcpdlist_detail.get(i).setLnum("1");
            zcpdlist_detail.get(i).setLdiffnum("" + (1 - Integer.valueOf(zcpdlist_detail.get(i).getLbuildnum())));
            zcpdlist_detail.get(i).setIsup("1");
            btn1_tv.setText("保存(已修改)");
            //更新界面
            insert_data(i);
        } else {
            //不存在则提示,并修改扫描框为全部选中状态.方便用户重新扫描。
            AbToastUtil.showToastInThread(getApplicationContext(), "该资产不在盘点列表中!");
            //20161008 清空列表中内容，便于区分是否扫描
            scardnum_tv.setText("");
            sassetnum_tv.setText("");
            sassetname_tv.setText("");
            sstyle_tv.setText("");
            dblvalue_tv.setText("");
            sdeptnum_tv.setText("");
            sdeptnum_tv.setTag("");
            ssite_tv.setText("");
            //可修改部分
            creason_et.setText("");
            RadioGroup lnum_rg = (RadioGroup) findViewById(R.id.lnum_rg);
            lnum_rg.check(R.id.no_rb);
            code_et.selectAll();
            return false;
        }
        return true;
    }

    public void showwin(boolean stype) {
        //背景变白，字变黑色
        creason_et.setEnabled(stype);
        if (stype) {
            //iquantity_tv.setBackgroundResource(R.drawable.edit_background);
            creason_et.setBackgroundResource(R.drawable.edit_background);
        } else {
            int textcolor = textcolor_old;
            Integer backgroundcolor = Color.TRANSPARENT;

            creason_et.setTextColor(textcolor);
            creason_et.setBackgroundColor(backgroundcolor);
        }
    }

    //对一些修改后的值进行处理,避免因为没触发事件导致保存的信息不正确
    public void updatedataref() {
        // 修改为状态为已盘点
        RadioButton yes_rb = (RadioButton) findViewById(R.id.yes_rb);
        String creason = zcpvouchs.getCreason();
        String lnum = zcpvouchs.getLnum();
        if (creason_et.getText() != null) {
            zcpvouchs.setCreason(creason_et.getText().toString());
        } else {
            zcpvouchs.setCreason("");
        }

        //是否通过
        if (yes_rb.isChecked()) {
            zcpvouchs.setLnum("1");
            zcpvouchs.setLdiffnum("" + (1 - Integer.valueOf(zcpvouchs.getLbuildnum())));
        } else {
            zcpvouchs.setLnum("0");
            zcpvouchs.setLdiffnum("" + (0 - Integer.valueOf(zcpvouchs.getLbuildnum())));
        }
        //是否发生修改的判断
        if (!zcpvouchs.getCreason().equals(creason) || !zcpvouchs.getLnum().equals(lnum)) {
            zcpvouchs.setIsup("1"); //设置为已修改
            btn1_tv.setText("保存(已修改)");
        }
    }

    //保存盘点单
    public void btn1_click(View v) {
        updatedataref();
        //loadingDialog.loading(this);
        //20160817调整，只提交发生修改的资产
        ArrayList<Htzc_Pvouchs> zcpdlist_save = new ArrayList<Htzc_Pvouchs>();
        //复制发生修改的数据到新的list中
        for (int i = 0; i < zcpdlist_detail.size(); i++) {
            Htzc_Pvouchs zcpd = zcpdlist_detail.get(i);
//            if (zcpd.getIsup().equals("1")) {
            zcpdlist_save.add(zcpd);
//            }
        }
        String urlString = "http://" + InstorageURL.SERVERURL
                + BaseUrl_New;
        String[] parms = {"user", "cacc_id", "iyear", "cvouchid"};
        String[] vals = {User.nickName, (String) myApplication.getGs_ztid(), (String) myApplication.getGs_year(), zcpdtitle.getCvouchid()};
        //生成xml
        BuildHtzcPvouchsXMLService service = new BuildHtzcPvouchsXMLService();
        String xmlstring = service.buildXML(zcpdlist_save, "ZcpdSave81", InstorageURL.NAME_SPACE, parms, vals, "in" + vals.length);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "ZcpdSave81", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    //解析xml文件,返回列表
                    List<Htzc_Pvouchs> relist = XMLHtzcPvouchslist.paraseCommentInfors(result);
                    //默认显示第一行数据
                    if (relist.size() > 0) {
                        Htzc_Pvouchs re = relist.get(0);
                        if (re.getSuccount().equals("1")) {
                            AbToastUtil.showToastInThread(getApplicationContext(), "保存成功,可以继续盘点扫描!");
                            //20160817修改为保存后只修改当前列表中扫描修改状态，不退出界面
                            for (int i = 0; i < zcpdlist_detail.size(); i++) {
                                Htzc_Pvouchs zcpd = zcpdlist_detail.get(i);
                                if (zcpd.getIsup().equals("1")) {
                                    zcpd.setIsup("0");
                                }
                            }
                            //修改按钮名称
                            btn1_tv.setText("保存");
                            /*
                            //成功之后返回主界面并刷新
                            Intent intent = new Intent();
                            intent.putExtra("isref", "1");//是否需要刷新当前列表
                            setResult(R.layout.layout_zcpd_detail, intent);  //设置返回结果
                            finish();*/
                        } else if (re.getSuccount().equals("-102")) {
                            AbToastUtil.showToastInThread(getApplicationContext(), "已被盘到!");
                        } else if (re.getSuccount().equals("-101")) {
                            AbToastUtil.showToastInThread(getApplicationContext(), "没有找到有效的待盘点数据!");
                        } else if (re.getSuccount().equals("-1")) {
                            AbToastUtil.showToastInThread(getApplicationContext(), "没有修改!");
                        }
                    } else {
                        AbToastUtil.showToastInThread(getApplicationContext(), "保存失败!");
                    }
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), "保存失败!数据库错误!");
                }
                //loadingDialog.loadingfinish();
            }

            @Override
            public void onFailure(String result) {
                AbToastUtil.showToastInThread(getApplicationContext(), "保存失败!");
                //loadingDialog.loadingfinish();
            }
        });
    }

    public void btn2_click(View v) {
        Intent intent = new Intent(getApplicationContext(), Basenvo_ChooseXzpdperson.class);
        startActivityForResult(intent, R.layout.basenvo_rchoose / 200000);
    }

    public void zcpdAssignAssistCchecker(String cuser_name, String cuser_id) {
        String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
        AbSoapParams params = new AbSoapParams();
        params.put("arg0", User.nickName);
        params.put("arg1", myApplication.getGs_ztid());
        params.put("arg2", myApplication.getGs_year());
        params.put("arg3", zcpdtitle.getCvouchid());
        params.put("arg4", cuser_id);
        params.put("arg5", cuser_name);
        mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "ZcpdAssignAssistCchecker", params, new AbSoapListener() {
            //获取数据成功会调用这里
            @Override
            public void onSuccess(int statusCode, String content) {
                if (content != null) {
                    List<Stringreturn> relist = abXMLZcpdPersonslist.paraseCommentInfors(content);
                    //默认显示第一行数据
                    if (relist.size() > 0) {
                        Stringreturn re = relist.get(0);
                        if (re.getCdefine1().equals("1")) {
                            Toast.makeText(getApplicationContext(), "指派成功!", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), re.getCdefine2(), Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "指派失败!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    AbToastUtil.showToast(zcpd_detail.this, R.string.sqlerrormsg);
                }
            }

            // 失败，调用
            @Override
            public void onFailure(int statusCode, String content,
                                  Throwable error) {
                AbToastUtil.showToast(zcpd_detail.this, R.string.sqlerrormsg);
            }

            // 开始执行前
            @Override
            public void onStart() {
                //显示进度框
            }

            // 完成后调用，失败，成功
            @Override
            public void onFinish() {
                //移除进度框
                AbDialogUtil.removeDialog(zcpd_detail.this);
            }
        });
    }

    //接收返回数据(供应商和存货明细)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.layout_zcpd_detaillist / 300000:
                Bundle bundle = data.getExtras();
                String listrow = bundle.getString("listrow");
                if (listrow.equals("-100")) {
                    //退回成功,关闭当前,并刷新列表
                    Intent intent = new Intent();
                    intent.putExtra("isref", "1");//是否需要刷新当前列表
                    setResult(R.layout.layout_zcpd_detail / 200000, intent);  //设置返回结果
                    this.finish();
                } else {
                    //更换明细
                    zcpdlist_detail = (ArrayList<Htzc_Pvouchs>) data.getSerializableExtra("detaillist");
                    zcpdtitle = (Htzc_Pvouch) data.getSerializableExtra("zcpdtitle");
                    insert_data(Integer.valueOf(listrow));
                }
                break;
            case R.layout.basenvo_rchoose / 200000:
                Bundle bundle_person = data.getExtras();
                String cuser_name = bundle_person.getString("cuser_name");
                String cuser_id = bundle_person.getString("cuser_id");
                zcpdAssignAssistCchecker(cuser_name, cuser_id);
                break;
            //照相机扫描
            case R.layout.basenvo_capture:
                if (data != null) {
                    Bundle bundle_capture = data.getExtras();
                    String sc_code = bundle_capture.getString("result");
                    //增加二维码处理,根据文字中是否存在换行符号进行分割；1维码通过分割也能获取1条数据
                    String st1[] = sc_code.split("\r\n");
                    //资产编码必须在二维码的开头，然后通过：（全角）与文字进行分割(先要做个中文乱码转换)
                    /*
                    String st2[] = Basenvo.Iso2Utf8(st1[0]).split("：");//20161008采用新方式
                    if (st2.length == 1){
                        //只有1组，则是1维码
                        sc_code = st2[0];
                    }else{
                        //有多组，则默认第一个：之后的内容就是资产编号
                        sc_code = st2[1];
                    }
                    *///20161008 采用新方式，找到第一个非中文字符后，认为剩余字符是合法的条码内容
                    sc_code = Basenvo.StringLoseChinese(st1[0]);
                    //Log.i("3", sc_code);
                    //转换完成
                    code_et.setText(sc_code);
                    if (!sc_code.equals("")) {
                        //处理数据
                        input_code();
                    }
                }
                break;
            case R.string.camera_turnback://切换摄像头
                Bundle bundle_turnback = data.getExtras();
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), Basenvo_Capture.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtras(bundle_turnback);
                startActivityForResult(intent, 1);
                break;
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //向左翻页
    public void btn_abs_back(View v) {
        updatedataref();
        if (icurrentrow == 0) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是第一项");
        } else {
            insert_data(icurrentrow - 1);
        }
    }

    //向右翻页
    public void btn_abs_forward(View v) {
        updatedataref();
        if (icurrentrow == (zcpdlist_detail.size() - 1)) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是最后一项");
        } else {
            insert_data(icurrentrow + 1);
        }
    }
}
